
<template>
	<view >		
		<scroll-view scroll-y class="page">
			<cu-custom bgColor="bg-gradual-pink" :isBack="true">
				<block slot="backText"></block>
				<block slot="content">选择抄表本</block>				
			</cu-custom>
			<!-- list列表 -->
			<view style="padding: 0 10px 0  10px;">
				<view class="cu-list menu" >
					<app-select label="抄表月份：" v-model="formModel.readMonth" placeholder="请选择抄表月份" :dict="dictOptions" space ></app-select>
				</view>				
			</view>
			<view v-for="(item,index) in treeData" :key="index" >
				<uni-card :title="item.bookName" @click="onEnter(item.bookId,item.meters,item.readeds)">
					<template>
						<view class="content">
							<text class="text-green">总户数：{{item.meters}}</text> 
							<text class="text-blue" style="margin-left: 15px;">已抄：{{item.readeds}}</text>
							<text class="text-red" style="margin-left: 15px;">未抄：{{item.meters - item.readeds}}</text>
						</view>		
					</template>
				</uni-card>
			</view>
			
		<!-- <view class="padding flex flex-direction" >
			<button class="cu-btn bg-green lg"  @click="onEnter">进入抄表</button>
		</view> -->
	

		</scroll-view>
	</view>
</template>

<script>
	import api from '@/api/api.js'
	import util from './util'
	import appSelect from '@/components/my-componets/appSelect.vue'
	export default {
		components:{
		    appSelect
		 },
		data() {
			return {
				toggleStatus: false,
				loading: false,
				formModel:{
					readMonth: '',
					bookId: '',
					meters: 0,
					readeds: 0					
				}, 
				treeData: [],
				dictOptions:[],
				bookUrl:'/mr/book/queryBookReads',
			};
		},
		onLoad() {			
		},
		created(){
			this.loadDict();
			this.loadinfo();
		},
		mounted(){	
		    util.$on('readAdd',(bookId)=>{
				console.log("testAdd",bookId);
				this.updateReadedAdd(bookId);
			}),
			util.$on('readDel',(bookId)=>{
				console.log("testDel",bookId);
				this.updateReadedDel(bookId);
			})
		},
		methods: {
			updateReadedAdd(bookId){				
				for(let i=0;i<this.treeData.length;i++){
					let bObj= this.treeData[i];
					if(bObj.bookId == bookId){
						if(bObj.readeds+1 <= bObj.meters){
							this.treeData[i].readeds = this.treeData[i].readeds+1;
						}						
					}					
				}
			},
			updateReadedDel(bookId){
				for(let i=0;i<this.treeData.length;i++){
					let bObj= this.treeData[i];
					if(bObj.bookId == bookId){
						if(bObj.readeds-1 >= 0){
							this.treeData[i].readeds = this.treeData[i].readeds-1;
						}
					}
				}				
			},
			onEnter(bookId,meters,readeds){
				if(bookId){
					this.formModel.bookId=bookId;
					this.formModel.meters=meters;
					this.formModel.readeds=readeds;
					console.log("this.formModel",this.formModel)
					this.$Router.push({name:'readRecord', params: this.formModel})
				}
				/* uni.navigateTo({
					url: '/pages/user/useredit?item='+item
				}); */
			},
			loadDict(){
				let getDayMonth = this.getDayMonth();
				let getLastMonth = this.getLastMonth();
				
				let dictOp=[
					{text:getDayMonth,value:getDayMonth},
					{text:getLastMonth,value:getLastMonth}
				]			
				this.dictOptions=dictOp;
				this.formModel.readMonth=getDayMonth;
			},
			loadinfo(){
				//console.log("query:",this.feeParams);				
				this.$http.get(this.bookUrl,{params:{readMonth:this.formModel.readMonth}}
					).then(res=> {
					if (res.data.success) {
						this.treeData = res.data.result;
					}					
				}).catch(e=>{
					console.log("请求错误",e)
				})
			},
			getDayMonth() {
				var date = new Date(),
					year = date.getFullYear(),
					month = date.getMonth() + 1,
					day = date.getDate();
				month= month >= 1 && month <= 9 ? ("0" + month) : month;
				day = day >= 0 && day <= 9 ? ("0" + day) : day ;
				var timer = year + month;
				return timer;
			},
			getLastMonth() {
				var date = new Date(),
					year = date.getFullYear(),
					month = date.getMonth()+1;
					if(month == 1){
						year = year-1;
						month=12;
					}else{
						month=month-1;
						month= month >= 1 && month <= 9 ? ("0" + month) : month;
					}		
				var timer = year + month;
				return timer;
			}
			
		}
	}
</script>

<style>
	
</style>
